<?php
namespace Admin\Controller;
use \Admin\Model\DonateModel;
include "./Common/CheckAdmin.php";
final class DonateController{
	//资金捐赠列表
	public function moneyDonateList(){
		//实例化UserModel对象
		$donateModelObj = new DonateModel();
		
		//以下为分页的代码
		//每页显示条数
		$pagesize = $GLOBALS['config']['pagesize'];
		//获取当前页面
		$page = isset($_GET['page'])?$_GET['page']:1;
		//获取开始行号(每一页的第一行应该是这一页页数-1然后乘以每页大小)
		$startrows = ($page-1)*$pagesize;
		//获取总记录数,数据库rowcount
		$rows = $donateModelObj->rowCountMoney();
		//计算总页数（所有行数，除以每页数量就应该是总行数。但是必须上取整，因为最后一页可能不足$pagesize的数量）
		$pages = ceil($rows/$pagesize);
		//开始页码
		//结束页码
		$start = $page-5;
		$end = $page+4;
		if($page<=6){
			$start = 1;
			$end = 10;
		}
		if($page>=$pages-4){
			$start = $pages-9;
			$end = $pages;
		}
		if($pages<10){
			$start = 1;
			$end = $pages;
		}

		//分页获取记录
		$arrs = $donateModelObj->fetchMoneyByPages($startrows,$pagesize);	
		//引入视图文件
		include VIEW_PATH."moneyDonateList.html";
	}
	
	//书籍捐赠列表
	public function bookDonateList(){
		//实例化UserModel对象
		$donateModelObj = new DonateModel();
		
		//以下为分页的代码
		//每页显示条数
		$pagesize = $GLOBALS['config']['pagesize'];
		//获取当前页面
		$page = isset($_GET['page'])?$_GET['page']:1;
		//获取开始行号(每一页的第一行应该是这一页页数-1然后乘以每页大小)
		$startrows = ($page-1)*$pagesize;
		//获取总记录数,数据库rowcount
		$rows = $donateModelObj->rowCountBook();
		//计算总页数（所有行数，除以每页数量就应该是总行数。但是必须上取整，因为最后一页可能不足$pagesize的数量）
		$pages = ceil($rows/$pagesize);
		//开始页码
		//结束页码
		$start = $page-5;
		$end = $page+4;
		if($page<=6){
			$start = 1;
			$end = 10;
		}
		if($page>=$pages-4){
			$start = $pages-9;
			$end = $pages;
		}
		if($pages<10){
			$start = 1;
			$end = $pages;
		}

		//分页获取记录
		$arrs = $donateModelObj->fetchBookByPages($startrows,$pagesize);	
		//引入视图文件
		include VIEW_PATH."bookDonateList.html";
	}
	
	//加载某一捐书记录的详情列表
	public function donateBookDetailList(){
		$id = $_GET['id'];
		$donateModelObj = new DonateModel();
		$arrs = $donateModelObj -> getDonateBookDetailList($id);
		include VIEW_PATH."donateBookDetailList.html";
	}
	
	//对具体的捐书明细记录进行入库的方法
	public function setDonateBookDetailIn(){
		$id = $_GET['id'];
		$donateModelObj = new DonateModel();
		$arrs = $donateModelObj -> getDonateBookDetail($id);
		//$book_name = $arrs[0]['book_name'];
		$isbn = $arrs['isbn'];
		//用该书的ISBN到love_book表去查找，有记录则说明库中已有该书，则在数量字段加上值，若记录中没有，则新增一条love_book记录
		$flag = $donateModelObj -> isExistBook($isbn);
		if($flag==0){
			//新增
			//获取结果集存放到变量
			$book_name = $arrs['book_name'];
			$book_isbn = $arrs['isbn'];
			$book_count = $arrs['book_count'];
			$row = $donateModelObj -> addNewBook($book_name,$book_isbn,$book_count);
			if($row==1){
				//设明细表中该记录为已入库
				$donateModelObj -> setDetailIn($id);
				//然后输出信息
				$mes = [
				"code"=>200,
				"msg"=>"入库成功<br>",
				"url"=>"?c=Donate&a=donateBookDetailList&id=$id"
				];
				$json =  json_encode($mes);
				echo $json;
			}else{
				//报错
				$mes = [
				"code"=>500,
				"msg"=>"入库失败",
				//"url"=>"?c=User&a=showUser"
				];
				$json =  json_encode($mes);
				echo $json;
			}
		}else{
			//加值
			//获取该书的书籍ID,book_id
			$idarr = $donateModelObj -> getBookId($isbn);
			$book_id = $idarr['book_id'];
			$book_count = $arrs['book_count'];
			$row = $donateModelObj -> updateBookInventory($book_id,$book_count);
			if($row==1){
				//设明细表中该记录为已入库
				$donateModelObj -> setDetailIn($id);
				//然后输出信息
				$mes = [
				"code"=>200,
				"msg"=>"入库成功<br>",
				"url"=>"?c=Donate&a=donateBookDetailList&id=$id"
				];
				$json =  json_encode($mes);
				echo $json;
			}else{
				//报错
				$mes = [
				"code"=>500,
				"msg"=>"入库失败",
				//"url"=>"?c=User&a=showUser"
				];
				$json =  json_encode($mes);
				echo $json;
			}
		}
	}
	
	//设置状态的几个方法
	//收书
	public function setDonateBookReceived(){
		$id = $_GET['id'];
		$donateModelObj = new DonateModel();
		$row = $donateModelObj -> setDonateBookReceived($id);
		if($row==1){
			$mes = [
				"code"=>200,
				"msg"=>"成功设为已收书<br>",
			];
			$json =  json_encode($mes);
			echo $json;
		}
	}
	//入库
	public function setDonateBookIn(){
		$id = $_GET['id'];
		$donateModelObj = new DonateModel();
		$row = $donateModelObj -> setDonateBookIn($id);
		if($row==1){
			$mes = [
				"code"=>200,
				"msg"=>"成功设为已入库<br>",
			];
			$json =  json_encode($mes);
			echo $json;
		}
	}
	//异常
	public function setDonateBookErr(){
		$id = $_GET['id'];
		$donateModelObj = new DonateModel();
		$row = $donateModelObj -> setDonateBookErr($id);
		if($row==1){
			$mes = [
				"code"=>200,
				"msg"=>"成功设为异常<br>",
			];
			$json =  json_encode($mes);
			echo $json;
		}
	}
	//捐钱已收到
	public function setMoneyDonateCheck(){
		$id = $_GET['id'];
		$donateModelObj = new DonateModel();
		//查出该记录的捐钱数额
		$money = $donateModelObj -> fetchMoney($id);
		$money = $money['money'];
		//对common表的money修改
		$donateModelObj -> addMoney($money);
		$row = $donateModelObj -> setMoneyDonateCheck($id);
		if($row==1){
			$mes = [
				"code"=>200,
				"msg"=>"成功设为已支付<br>",
			];
			$json =  json_encode($mes);
			echo $json;
		}
	}
	//捐钱异常
	public function setMoneyDonateErr(){
		$id = $_GET['id'];
		$donateModelObj = new DonateModel();
		$row = $donateModelObj -> setMoneyDonateErr($id);
		if($row==1){
			$mes = [
				"code"=>200,
				"msg"=>"成功设为异常<br>",
			];
			$json =  json_encode($mes);
			echo $json;
		}
	}
}
?>